package com.paobuji.aidebao.myapplication.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.activity.BaseActivity;
import com.paobuji.aidebao.myapplication.appinterface.GetInterfaceObjctListener;
import com.paobuji.aidebao.myapplication.bean.UserBeanResult;
import com.paobuji.aidebao.myapplication.tool.DataTools;
import com.paobuji.aidebao.myapplication.tool.Default;
import com.paobuji.aidebao.myapplication.tool.MyIntentTools;
import com.paobuji.aidebao.myapplication.tool.ProgressDialog;
import com.paobuji.aidebao.myapplication.view.CustomTitlebar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FindPassActivity extends BaseActivity {

    ProgressDialog progressDialog;
    int randmm;
    Context context;

    @BindView(R.id.mainTitle)
    CustomTitlebar mainTitle;
    @BindView(R.id.userPhone)
    EditText userPhone;
    @BindView(R.id.keyPhone)
    EditText keyPhone;
    @BindView(R.id.sendKey)
    TextView sendKey;
    @BindView(R.id.userPass)
    EditText userPass;
    @BindView(R.id.userPassTwo)
    EditText userPassTwo;
    @BindView(R.id.userSubmit)
    Button userSubmit;

    MyIntentTools myIntentTools1;
    MyIntentTools myIntentTools2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_pass);
        ButterKnife.bind(this);
        //设置全透明
        StatusBarUtil.setTransparent(this);
        myIntentTools1 = new MyIntentTools();
        myIntentTools2 = new MyIntentTools();
        context = getApplicationContext();
        progressDialog = new ProgressDialog(this);
        //标题栏事件监测
        mainTitle.setAction(new CustomTitlebar.TitleBarOnClickListener() {
            @Override
            public void performAction(View view) {
                switch (view.getId()) {
                    case R.id.iv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "左边图片按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.iv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    /**
     * 发送验证码
     */
    public void sendSMS(String userPhone) {
        //splitStr();
        myIntentTools.SendSms(context,"send","1002912311@qq.com","123456","6397",userPhone,splitStr(),"json");
        myIntentTools1.setGetInterfaceObjctListener(new GetInterfaceObjctListener() {
            @Override
            public void getObjectDate(Object data) {
                countTime();     //倒计时
            }
        });
    }

    //六十秒倒计时
    public void countTime() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 60; i >= 0; i--) {
                    try {
                        Message msg = Message.obtain();

                        Thread.sleep(1000);

                        msg.arg1 = i;

                        hander.sendMessage(msg);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();
    }

    Handler hander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            sendKey.setText(msg.arg1 + " s" + R.string.resend);
            if (msg.arg1 == 0) {
                sendKey.setText( R.string.resend);
                sendKey.setClickable(true);
            }
        }
    };

    /**
     * 字符串拼接
     */
    public String splitStr() {
        randmm = DataTools.randomInt(100000, 999999);
        Log.i("验证码:","---------" + randmm + "---------");
        return Default.contentStr + randmm;
    }


    /**
     * 为空监测
     *
     * @param e
     * @return
     */
    public boolean chackEmpty(EditText e) {
        if ("".equals(e.getText().toString().trim()) || e.getText().toString().trim() == null) {
            toastLog(R.string.empty+"");
            return false;
        }
        return true;
    }

    /**
     * 跳转到登录界面
     */
    public void goLoginActivity() {
        Intent intent = new Intent(FindPassActivity.this, LoginActivity.class);
        startActivity(intent);
    }

    @OnClick({R.id.sendKey, R.id.userSubmit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sendKey:       //发送验证码
                if(chackEmpty(userPhone)){
                    sendSMS(userPhone.getText().toString().trim());
                }else{
                    toastLog(R.string.empty+"");
                }
                break;
            case R.id.userSubmit:         //找回
                if(keyPhone.getText().toString().trim().equals(String.valueOf(randmm))){
                    if(chackEmpty(userPass) && chackEmpty(userPassTwo)){
                        if(userPass.getText().toString().trim().equals(userPassTwo.getText().toString().trim())){
                            progressDialog.showDialog();
                            //注册用户
                            myIntentTools2.updateUserPass(userPhone.getText().toString().trim(),userPass.getText().toString().trim());
                            //注册成功的回调
                            myIntentTools2.setGetInterfaceObjctListener(new GetInterfaceObjctListener() {
                                @Override
                                public void getObjectDate(Object data) {
                                    if( ((UserBeanResult)data).getCode() == 0){
                                        //跳转到登录界面
                                        goLoginActivity();

                                    }else if(((UserBeanResult)data).getCode() == 2){
                                        progressDialog.cancelDialog();
                                        toastLog(R.string.exist + "");
                                    }else {
                                        progressDialog.cancelDialog();
                                    }

                                }
                            });
                        }else{
                            toastLog(R.string.erroneous + "");
                        }
                    }else {
                        toastLog(R.string.erroneous + "");
                    }
                }else{
                    toastLog(R.string.erroneous + "");
                }
                break;
        }
    }
}
